<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/GPL-3.0
 * @link https://www.gougucms.com
 */

declare (strict_types=1);

namespace app\cert\controller;

use app\base\BaseController;
use app\cert\model\CertSales;
use app\cert\model\SecurityAccount;
use think\facade\Db;
use think\facade\View;

class Certsale extends BaseController
{
    public function index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            $whereOr = array();
            if (!empty($param['keywords'])) {
                $where[] = ['a.company_name|a.client_name', 'like', '%' . $param['keywords'] . '%'];
            }

            $where[] = ['a.delete_time', '=', 0];
            if ($this->uid != 1 && $this->uid != 3) {
                $where[] = ['a.admin_id', '=', $this->uid];
            }
            $model = new CertSales();
            $list = $model->get_list($param, $where, $whereOr);
            return table_assign(0, '', $list);
        } else {

            return view();
        }
    }


    //添加&&编辑
    public function add()
    {
        $param = get_params();
        unset($param['file']);
        if(empty($param['sign_date'])){
            unset($param['sign_date']);
        }

        if (request()->isAjax()) {
            if (!empty($param['id']) && $param['id'] > 0) {
                $param['update_time'] = time();
                $old = Db::name('cert_sales')->where(['id' => $param['id']])->find();
                $model = new CertSales();
                $res = $model->field(true)->update($param);
                if ($res) {
                    add_log('edit', $param['id'], $param);
                    to_log($this->uid, $param, $old);
                    return to_assign();
                } else {
                    return to_assign(1, '操作失败');
                }
            } else {
                $param['create_time'] = time();
                $param['admin_id'] = $this->uid;
                $model = new CertSales();
                $aid = $model->field(true)->insertGetId($param);
                if ($aid) {
                    add_log('add', $aid, $param);
                    return to_assign();
                } else {
                    return to_assign(1, '操作失败');
                }
            }
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            View::assign('id', $id);
            if ($id > 0) {
                $model = new CertSales();
                $detail = $model->detail($id);
                View::assign('detail', $detail);
            }
            return view();
        }
    }

    //删除
    public function delete()
    {
        if (request()->isDelete()) {
            $id = get_params("id");
            $data['id'] = $id;
            $data['delete_time'] = time();
            if (Db::name('cert_sales')->update($data) !== false) {
                add_log('delete', $id);

                return to_assign(0, "删除成功");
            } else {
                return to_assign(1, "删除失败");
            }
        } else {
            return to_assign(1, "错误的请求");
        }
    }


    //添加&&编辑
    public function view()
    {
        $param = get_params();
        unset($param['file']);
        unset($param['scene']);
        if (request()->isAjax()) {

        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            View::assign('id', $id);
            if ($id > 0) {
                $model = new CertSales();
                $detail = $model->detail($id);
                View::assign('detail', $detail);
            }
            return view();
        }
    }
    public function report()
    {
        $param = get_params();

        if (request()->isAjax()) {
            $check_uid = isset($param['check_uid']) ? $param['check_uid'] : 0;
            $id = isset($param['id']) ? $param['id'] : 0;
            $data['id'] = $id;
            $data['check_uid'] = $check_uid;
            $data['status'] = 1;
            if (Db::name('cert_sales')->update($data) !== false) {
                return to_assign(0, "上报成功");
            } else {
                return to_assign(1, "上报失败");
            }

        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            View::assign('id', $id);
            if ($id > 0) {
                $model = new CertSales();
                $detail = $model->detail($id);
                View::assign('detail', $detail);
            }
            return view();
        }
    }
}
